class TeacherController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        def teacher = session.searchTeacher
        if (!params.max && !params.order) {
            teacher = new Teacher(params.findAll({k, v -> "" != v}))
            session.searchTeacher = teacher
        }
        if (!params.max) params.max = 10
        [ teacher:teacher,
          teacherList:Teacher.findAllEx(teacher, params),
          total:Teacher.countEx(teacher)]
    }

    def show = {
        def teacher = Teacher.get( params.id )

        if(!teacher) {
            flash.message = "teacher.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Teacher not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ teacher : teacher ] }
    }

    def delete = {
        def teacher = Teacher.get( params.id )
        if(teacher) {
            teacher.delete()
            flash.message = "teacher.deleted"
            flash.args = [params.id]
            flash.defaultMessage = "Teacher ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = "teacher.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Teacher not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def teacher = Teacher.get( params.id )

        if(!teacher) {
            flash.message = "teacher.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Teacher not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ teacher : teacher ]
        }
    }

    def update = {
        def teacher = Teacher.get( params.id )
        def dc = grailsApplication.getDomainClass("Teacher")
        Teacher.hasMany.each { k, v ->
        	def prop = dc.getPropertyByName(k)
	        if ((prop.manyToMany && prop.owningSide) || (prop.oneToMany && !prop.otherSide)) {
	    		def otherSideClass = prop.referencedPropertyType
	    		def name = prop.name
	    		def methodName = name[0].toUpperCase() + (name.size() ? name[1..name.size() - 1] : '')
        		def ids = params."${k}\$"?.id
        		ids = (ids?.class == String ? [ids] : ids?.toList()).collect({Long.valueOf(it)})
        		
   				def dels = teacher."${k}"?.id - ids
        		def adds = ids - dels
				dels.each { id ->
					teacher."removeFrom${methodName}"(otherSideClass.get(Long.valueOf(id)))
				}
				adds.each { id ->
					teacher."addTo${methodName}"(otherSideClass.get(Long.valueOf(id)))
				}
        	}
        }
        	
        if(teacher) {
            teacher.properties = params
            if(!teacher.hasErrors() && teacher.save()) {
                flash.message = "teacher.updated"
                flash.args = [params.id]
                flash.defaultMessage = "Teacher ${params.id} updated"

                Teacher.hasMany.each { k, v ->
                	def prop = dc.getPropertyByName(k)
                	if (prop.manyToMany && !prop.owningSide) {
        	    		def otherSideClass = prop.referencedPropertyType
        	    		def name = prop.otherSide.name
        	    		def methodName = name[0].toUpperCase() + name[1..name.size() - 1]
                		def ids = params."${k}\$"?.id
                		ids = (ids?.class == String ? [ids] : ids?.toList()).collect({Long.valueOf(it)})
                		
           				def dels = teacher."${k}"?.id - ids
                		def adds = ids - dels
        				dels.each { id ->
        				otherSideClass.get(Long.valueOf(id))."removeFrom${methodName}"(teacher).save()
        				}
        				adds.each { id ->
        					otherSideClass.get(Long.valueOf(id))."addTo${methodName}"(teacher).save()
        				}
                	}
                }
                	
                redirect(action:show,id:teacher.id)
            }
            else {
                render(view:'edit',model:[teacher:teacher])
            }
        }
        else {
            flash.message = "teacher.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Teacher not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def teacher = new Teacher()
        teacher.properties = params
        return ['teacher':teacher]
    }

    def save = {
        def teacher = new Teacher(params)

        def dc = grailsApplication.getDomainClass("Teacher")
        Teacher.hasMany.each { k, v ->
        	def prop = dc.getPropertyByName(k)
	        if ((prop.manyToMany && prop.owningSide) || (prop.oneToMany && !prop.otherSide)) {
	    		def otherSideClass = prop.referencedPropertyType
	    		def name = prop.name
	    		def methodName = name[0].toUpperCase() + name[1..name.size() - 1]
        		def ids = params."${k}\$"?.id
        		ids = (ids?.class == String ? [ids] : ids?.toList()).collect({Long.valueOf(it)})
				ids?.each { id ->
					teacher."addTo${methodName}"(otherSideClass.get(Long.valueOf(id)))
				}
	       	}
        }
        
        if(!teacher.hasErrors() && teacher.save()) {
            flash.message = "teacher.created"
            flash.args = ["${teacher.id}"]
            flash.defaultMessage = "Teacher ${teacher.id} created"

            Teacher.hasMany.each { k, v ->
            	def prop = dc.getPropertyByName(k)
            	if (prop.manyToMany && !prop.owningSide) {
    	    		def otherSideClass = prop.referencedPropertyType
    	    		def name = prop.otherSide.name
    	    		def methodName = name[0].toUpperCase() + (name.size() ? name[1..name.size() - 1] : '')
            		def ids = params."${k}\$"?.id
            		ids = (ids?.class == String ? [ids] : ids?.toList()).collect({Long.valueOf(it)})
    				ids?.each { id ->
    					otherSideClass.get(Long.valueOf(id))."addTo${methodName}"(teacher).save()
    				}
            	}
            }
        	
            redirect(action:show,id:teacher.id)
        }
        else {
            render(view:'create',model:[teacher:teacher])
        }
    }
}